<?php namespace Common\Model;
use Hdphp\Model\Model;

class User extends Model{
	protected $table="user";
	//自动验证
	protected $validate=array(
		//array(字段名，验证方法，错误信息，验证条件，验证时间)
		array('username','required','用户名必填',3,3),
		array('upassword','required','密码必填',3,3),
		array('upassword','minlen:6','密码不能少于6位',3,3),
		array('code','required','验证码必填',3,3),
		array('code','_code','验证码不正确',3,3),
		array('username','_username','该用户已注册',3,3),

	);
	//自动完成
	protected $auto=array(
		array('upassword','md5','function',3,3)
	);
	/**
	 * 验证验证码是否正确
	 */
	public function _code(){
        $code = Q('post.code');
		if(strtolower($code)!=strtolower($_SESSION['code'])){
			return false;
		}
		return true;
	}
	/**
	 * 验证用户名是否存在
	 */
	 public function _username(){
	 	$username=Q('post.username');
//		if($username!=)
//		$data=$this->where("'username'=>$username")->find();
        $data = $this->where(['username'=>$username])->find();

		if($data) return false;
		return true;
	 }
	/*
	 *自动验证的自定义方法,判断用户名是否存在
	 */
	public function store(){
		if($this->create()){
			return $this->add();
		}
		return false;
	}
}